<?php
/**
 * 
 * 检查垃圾uid
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}


/*ini_set("display_errors",ture);
error_reporting(E_ALL);*/

define("CRON_NAME","check_laji_uid");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");

class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		$db_fanlaji_laji_uid = new db_fanlaji_laji_uid();
		$mblog_from_api = new mblog_from_api();
		
		//获取tag的总数
		$count = $db_fanlaji_laji_uid->cron_get_laji_uid_count($this->start_id);
		
		if($count == 0)
		{
			//如果获取不到需要更新的数据，初始化cron_start_id
			$this->ini_start_id(NOW_PROCESS);
			
			$this->start_id = 0 ;
			$count = $db_fanlaji_laji_uid->cron_get_laji_uid_count($this->start_id);
		}
		
		
		echo "tag_count:$count<br>\n";
		$total_page = ceil($count/PCOUNT);
		if($total_page > MAX_PAGE && defined("MAX_PAGE")) $total_page = MAX_PAGE;
			
		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		if($count < PCOUNT)
		{
			$this->ini_start_id(NOW_PROCESS);
		}
	}	
	
	function get_list_by_page($p)
	{
		$db_fanlaji_laji_uid = new db_fanlaji_laji_uid();
		
		//分批次取tag名称
		$food_tag = $db_fanlaji_laji_uid->cron_get_laji_uid_list($this->start_id,$p,PCOUNT);
		
		foreach($food_tag as $value)
		{
			$laji_uid = $value["laji_uid"];
			$id = $value["id"];
			if($id % TOTAL_PROCESS != NOW_PROCESS)//用总进程做控制 example: 3个进程  php xxx 1 只会执行除3余1的id
			{
				continue;
			}
			
			if($laji_uid)//处理抓取分页逻辑
			{
				$this->get_list_by_topic($laji_uid);
				
			}
			
			if($id)//更新抓取的start_id到数据库
			{
				$done_job = true;
				$res = $this->replace_into_data($id);	
			}
			
			
		}
		
		if($done_job == false)
		{
			$this->ini_start_id();
		}
			
	}
	
	
	//根据话题抓取数据并入库
	function get_list_by_topic($laji_uid)
	{
		global $mid_have_get;
		$max_time = "";
		//执行时间超过30分钟，退出
		if(time() - CRON_START_TIME > 900)
		{
			exit;
		}
		$db_base = new db_base();
		$mblog_from_api = new mblog_from_api();
		
		
		$laji_user_info = $mblog_from_api->api_get_user_info_by_uid($laji_uid);

		if($laji_user_info["data"]["errno"] == 20003)
		{
			//用户不存在
			
			$db_fanlaji_laji_uid = new db_fanlaji_laji_uid();
			$db_fanlaji_laji_uid->update_fanlaji_laji_uid($laji_uid,1);
			
			$db_fanlaji_jubao_uid = new db_fanlaji_jubao_uid();
			$db_fanlaji_jubao_uid->update_fanlaji_jubao_uid_by_laji_uid($laji_uid,1);
			
		}
		
		
	}
		
			
		
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
